package cn.herlioner.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import cn.herlioner.dao.TeacherDAO;
import cn.herlioner.pojo.TSObject;
import cn.herlioner.pojo.Teacher;

public class TeacherDAOImpl implements TeacherDAO{

	@Override
	public void add(Teacher t) {
		SessionFactory sf = new Configuration().configure()
				.buildSessionFactory();
		Session s = sf.openSession();
		s.beginTransaction();
		
		s.save(t);
		
		s.getTransaction().commit();
		s.close();
		sf.close();
		
	}

	@Override
	public Teacher get(Long teanum) {
		Teacher result = null;
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        result = (Teacher) s.get(Teacher.class, teanum);
 
        s.close();
        sf.close();
        return result;
	}

	@Override
	public void delete(Long teanum) {
		SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
        s.beginTransaction();
 
        Teacher t = (Teacher) s.get(Teacher.class, teanum);
 
        s.delete(t);
 
        s.getTransaction().commit();
        s.close();
        sf.close();
		
	}

	@Override
	public void update(Teacher t) {
		SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
        s.beginTransaction();
 
        s.update(t);
 
        s.getTransaction().commit();
        s.close();
        sf.close();
		
	}

	@Override
	public List<Teacher> list() {
		List<Teacher> result = new ArrayList();
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        Query q = s.createQuery("from Teacher t");
 
        result = q.list();
 
        s.close();
        sf.close();
        return result;
	}

}
